package com.itheima.dao;

import com.itheima.domain.Role;
import com.itheima.domain.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserDao {

    /*
      登录，根据用户名查询
       */
    @Select("select * from users where username = #{username}")
    @Results({
            @Result(property = "id",column = "id"),
            @Result(property ="roles" , column = "id" ,many = @Many(select = "com.itheima.dao.RoleDao.findRoleByUserId"))
    })
    public UserInfo findByUsername(String username);

    /*
    查询用户管理
     */
    @Select("select * from users")
    public List<UserInfo> findAll();

    /*
    添加用户管理
     */
    @Insert("insert into users(email,username,password,phoneNum,status) " +
            "values(#{email},#{username},#{password},#{phoneNum},#{status})")
    public void save(UserInfo userInfo);

    /*
    根据id查询详情
     */
    @Select("select * from users where id = #{id}")
    @Results({
            @Result(id = true ,property = "id" ,column = "id"),
            @Result(property = "roles" ,column = "id" ,many=@Many(select = "com.itheima.dao.RoleDao.findRoleByUserId"))
    })
    public UserInfo findById(String id);

    /*
    根据id查询角色
     */
    @Select("select * from role where id not in(select roleId from users_role where userId = #{userId})")
    public List<Role> findOtherRoles(String userId);

    /*
    添加角色
     */
    @Insert("insert into users_role(userId,roleId) values(#{userId},#{roleId}) ")
    public void addRoleToUser(@Param("userId") String userId,@Param("roleId") String roleId);
}
